<template>
  <div class="login-line" ref="loginLineRef"></div>
</template>

<script setup lang="ts">
import { getloginCountAPI } from '@/api/overview'
import * as echarts from 'echarts'

import { onBeforeUnmount, onMounted, ref } from 'vue'
let handleResize = () => {}
const loginLineRef = ref()
const loginLine = async () => {
  data.value = await getloginCountAPI()
  const values = ref<number[]>([])
  const keys = ref<string[]>([])
  data.value.data.forEach((item: { name: string; value: number }) => {
    keys.value.push(item.name)
    values.value.push(item.value)
  })

  const myloginLine = echarts.init(loginLineRef.value)
  myloginLine.setOption({
    xAxis: {
      type: 'category',
      data: keys.value
    },
    yAxis: {
      type: 'value'
    },
    grid: {
      left: '3%',
      right: '6%',
      bottom: '3%',
      containLabel: true
    },
    series: [
      {
        data: values.value,
        type: 'line'
      }
    ]
  })
  const handleResize = () => {
    myloginLine.resize()
  }
  window.addEventListener('resize', handleResize)
}

// 使用函数并打印结果
const data = ref()

onMounted(() => {
  loginLine()
})
onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped></style>
